#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QList>
#include <QHostAddress>
#include <QHostInfo>
#include <QFont>
#include <QNetworkInterface>

#include "networkmanager.h"

NetWorkManager::NetWorkManager(QWidget *parent) : QWidget(parent)
{
    uiInit();
    setup();
}

void NetWorkManager::uiInit()
{
    QFont labelFont;
    labelFont.setBold(true);
    labelFont.setPointSize(18);

    titleName = new QLabel(this);
    titleName->setText("Smart PDU");
    titleName->setAlignment(Qt::AlignCenter);
    titleName->setFixedSize(240, 50);
    titleName->setFont(labelFont);
    titleName->setStyleSheet("background-color:#000080;color:white;");

    // eth0
    eth0Name = new QLabel(this);
    eth0Name->setText("ETH0:");
    eth0Name->setAlignment(Qt::AlignLeft);
    eth0Name->setFont(labelFont);
    eth0Name->setFixedSize(70, 30);
    eth0Name->setStyleSheet("color:white;");
    eth0IpAddress = new QLabel(this);
    eth0IpAddress->setAlignment(Qt::AlignLeft);
    eth0IpAddress->setFont(labelFont);
    eth0IpAddress->setText("0.0.0.0");
    eth0IpAddress->setFixedHeight(30);
    eth0IpAddress->setStyleSheet("color:white;");

    // eth1
    eth1Name = new QLabel(this);
    eth1Name->setText("ETH1:");
    eth1Name->setAlignment(Qt::AlignLeft);
    eth1Name->setFont(labelFont);
    eth1Name->setFixedSize(70, 30);
    eth1Name->setStyleSheet("color:white;");
    eth1IpAddress = new QLabel(this);
    eth1IpAddress->setAlignment(Qt::AlignLeft);
    eth1IpAddress->setFont(labelFont);
    eth1IpAddress->setText("0.0.0.0");
    eth1IpAddress->setFixedHeight(30);
    eth1IpAddress->setStyleSheet("color:white;");


    //eth0 layout
    QHBoxLayout * eth0Layout = new QHBoxLayout;
    eth0Layout->addWidget(eth0Name);
    eth0Layout->addWidget(eth0IpAddress);
    eth0Layout->setAlignment(Qt::AlignLeft);

    //eth1 layout
    QHBoxLayout * eth1Layout = new QHBoxLayout;
    eth1Layout->addWidget(eth1Name);
    eth1Layout->addWidget(eth1IpAddress);
    eth1Layout->setAlignment(Qt::AlignLeft);

    // mainLayout
    QVBoxLayout * mainLayout = new QVBoxLayout;

    mainLayout->setAlignment(Qt::AlignCenter);
    mainLayout->addLayout(eth0Layout);
    mainLayout->addLayout(eth1Layout);

    setLayout(mainLayout);
}

void NetWorkManager::setup()
{
    // eth0 getIP Address
    QString ip1 = getInterfaceAddress("wlp4s0");
    if (!ip1.isEmpty())
        eth0IpAddress->setText(ip1);
    // eth1 getIP Address
    QString ip2 = getInterfaceAddress("wlp4s0");
    if (!ip2.isEmpty())
        eth1IpAddress->setText(ip2);
}

QString NetWorkManager::getInterfaceAddress(const QString &interfaceName)
{
    QString result;
    foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces()) {
        if (interface.name() == interfaceName) {
            qDebug() << "name:" << interface.name();
            QList<QNetworkAddressEntry> entryList = interface.addressEntries();
            foreach(QNetworkAddressEntry entry, entryList) {
                if (entry.ip().toIPv4Address()) {
                    eth0IpAddress->setText(entry.ip().toString());
                    break;
                }
            }
            break;
        }
    }
    return result;
}
